package handler.store

import service.store.PluginManager

import com.github.kevinsawicki.http.HttpRequest

// plugin
// server side api
handler.get('/wx/store/plugin/list'){req, resp ->
	String q = req.params.q

	def dir = new File(PluginManager.pluginDir)
	def ll = dir.listFiles().grep{
		boolean flag = it.isDirectory() && !(it.name in ['tmp-install'])
		if(!flag)
			return flag

		q ? it.name.contains(q) : true
	}.collect{
		PluginManager.getPluginPack(it.name)
	}.grep{it}

	resp.json([ll: ll])
}

handler.get('/wx/store/plugin/list-installed'){req, resp ->
	def installed = PluginManager.getPluginInstalled()
	resp.json([installed: installed])
}

// download a plugin zip
handler.get('/wx/store/plugin/download'){req, resp ->
	String code = req.params.code
	String version = req.params.version
	if(!code || !version){
		resp.json([error: 'param-invalid'])
		return
	}

	String pluginName = code + '-' + version + '.zip'
	String packFilePath = PluginManager.pluginDir + '/' + pluginName

	def f = new File(packFilePath)
	if(!f.exists()){
		resp.json([error: 'plugin-invalid'])
		return
	}

	resp.download(pluginName, f.bytes, null)
}

// zip a plugin
handler.get('/wx/store/plugin/pack'){req, resp ->
	String code = req.params.code
	if(!code){
		resp.json([error: 'param-invalid'])
		return
	}

	def pack = PluginManager.getPluginPack(code)
	if(!pack){
		resp.json([error: 'plugin-invalid'])
		return
	}

	def data = PluginManager.pack(config, pack)
	resp.json data
}

// install from local, if mirror given download from remote first
handler.get('/wx/store/plugin/install'){req, resp ->
	String code = req.params.code
	String version = req.params.version
	if(!code || !version){
		resp.json([error: 'param-invalid'])
		return
	}

	String packFilePath = PluginManager.pluginDir + '/' + code + '-' + version + '.zip'

	String mirror = req.params.mirror
	if(mirror){
		// first download
		String url = mirror + 'download?code=' + code + '&version=' + version

		try{
			def bytes = HttpRequest.get(url).bytes()
			new File(packFilePath).setBytes(bytes)
		}catch(e){
			log.error('plugin-install-download-error', e)
			resp.json([flag: false, msg: e.message])
			return
		}
	}

	if(!new File(packFilePath).exists()){
		resp.json([error: 'plugin-invalid'])
		return
	}

	def pack = [code: code, version: version]
	boolean isUpdate = PluginManager.isPluginInstalled(pack)

	def r = isUpdate ? PluginManager.updatePack(pack) : PluginManager.installPack(pack)
	if(!r.flag){
		resp.json r
		return
	}

	PluginManager.addPluginAsInstalled(pack)
	resp.json r
}

// remove
handler.get('/wx/store/plugin/remove'){req, resp ->
	String code = req.params.code
	if(!code){
		resp.json([error: 'param-invalid'])
		return
	}

	def pack = PluginManager.getPluginPack(code)
	if(!pack){
		resp.json([error: 'plugin-invalid'])
		return
	}

	def r = PluginManager.removePack(pack)
	if(!r.flag){
		resp.json r
		return
	}

	PluginManager.removePluginAsInstalled(pack)
	resp.json r
}